﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.Plumbing
{
	public partial class FrmMarkRiserpipe : System.Windows.Forms.Form
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		public MarkType MarkingType { get; set; }

		public FrmMarkRiserpipe()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{2A63B0A7-FEA2-4471-B2B4-5B8CF761239A}", null);
		}

		private void FrmMarkRiserpipe_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.Icon = Resource.YArchitechIcon;
			this.radioSinglePipe.Checked = (MarkRiserPipeData.Instance.TypeOfMarking == MarkType.singlePipeMarking);
			this.radioMultiPipe.Checked = (MarkRiserPipeData.Instance.TypeOfMarking == MarkType.multiPipeMarking);
			this.textCustom.Enabled = this.radioCustom.Checked;
			this.checkAreaNumber.Checked = MarkRiserPipeData.Instance.IsUseAreaNumber;
			this.textAreaNumber.Enabled = this.checkAreaNumber.Checked;
			this.checkFloorNumber.Checked = MarkRiserPipeData.Instance.IsUseFloorNumber;
			this.textFloorNumber.Enabled = this.checkFloorNumber.Checked;
			this.checkLineAngle.Checked = MarkRiserPipeData.Instance.IsUseAngle;
			this.textLineAngle.Enabled = this.checkLineAngle.Checked;
			this.checkDisToPipe.Checked = MarkRiserPipeData.Instance.IsFixedDis;
			this.textDisToPipe.Enabled = this.checkDisToPipe.Checked;
			this.rboCurrentLevel.Checked = MarkRiserPipeData.Instance.IsCurrentLevel;
			this.rboCurrentSystem.Checked = !MarkRiserPipeData.Instance.IsCurrentLevel;
			this.checkBox1.Checked = true;
			this.textCustom.Text = MarkRiserPipeData.Instance.customNumber;
			this.textAreaNumber.Text = MarkRiserPipeData.Instance.AreaNumber;
			this.textFloorNumber.Text = MarkRiserPipeData.Instance.FloorNumber;
			this.textLineAngle.Text = MarkRiserPipeData.Instance.Angle;
			this.textDisToPipe.Text = MarkRiserPipeData.Instance.Distance;
			this.MarkingType = MarkRiserPipeData.Instance.TypeOfMarking;
			this.checkLineAngle.Enabled = this.radioSinglePipe.Checked;
			this.textLineAngle.Enabled = this.radioSinglePipe.Checked;
			this.checkDisToPipe.Enabled = this.radioSinglePipe.Checked;
			this.textDisToPipe.Enabled = this.radioSinglePipe.Checked;
			this.textLineAngle.Enabled = this.checkLineAngle.Checked;
			this.textDisToPipe.Enabled = this.checkDisToPipe.Checked;
			this.SelectThumbnail();
		}

		public void UpdatePipeMarkingNumber(string number)
		{
			if (!string.IsNullOrEmpty(number))
			{
				this.radioAutoIncrease.Checked = false;
				this.radioCustom.Checked = true;
				this.textCustom.Text = number.ToString();
				return;
			}
			this.radioAutoIncrease.Checked = true;
			this.radioCustom.Checked = false;
			this.textCustom.Text = "";
		}

		protected void SelectThumbnail()
		{
			bool @checked = this.radioSinglePipe.Checked;
			bool checked2 = this.radioMultiPipe.Checked;
			bool checked3 = this.checkAreaNumber.Checked;
			bool checked4 = this.checkFloorNumber.Checked;
			if (@checked && checked3 && checked4)
			{
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "S_WithAreaFloorInPluming.png"));
				return;
			}
			if (@checked && checked3 && !checked4)
			{
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "S_WithAreaInPluming.png"));
				return;
			}
			if (@checked && !checked3 && checked4)
			{
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "S_WithAreaInPluming.png"));
				return;
			}
			if (@checked && !checked3 && !checked4)
			{
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "S_WithoutAreaFloorInPluming.png"));
				return;
			}
			if (checked2 && checked3 && checked4)
			{
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "M_WithAreaFloorInPluming.png"));
				return;
			}
			if (checked2 && checked3 && !checked4)
			{
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "M_WithAreaInPluming.png"));
				return;
			}
			if (checked2 && !checked3 && checked4)
			{
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "M_WithAreaInPluming.png"));
				return;
			}
			if (checked2 && !checked3 && !checked4)
			{
				this.pictureBox1.Image = Image.FromFile(Path.Combine(Product.ResLocation, "M_WithoutAreaFloorInPluming.png"));
			}
		}

		private void radioSinglePipe_CheckedChanged(object sender, EventArgs e)
		{
			this.SelectThumbnail();
			if (this.radioSinglePipe.Checked)
			{
				this.MarkingType = MarkType.singlePipeMarking;
				this.isPicking = true;
				this.SwithStatus();
				this.isPicking = true;
			}
		}

		private void radioMultiPipe_CheckedChanged(object sender, EventArgs e)
		{
			this.SelectThumbnail();
			if (this.radioMultiPipe.Checked)
			{
				this.MarkingType = MarkType.multiPipeMarking;
				this.isPicking = true;
				this.SwithStatus();
				this.isPicking = true;
				this.checkLineAngle.Checked = false;
				this.checkDisToPipe.Checked = false;
			}
			this.checkLineAngle.Enabled = this.radioSinglePipe.Checked;
			this.textLineAngle.Enabled = this.radioSinglePipe.Checked;
			this.checkDisToPipe.Enabled = this.radioSinglePipe.Checked;
			this.textDisToPipe.Enabled = this.radioSinglePipe.Checked;
		}

		private void radioIncrease_CheckedChanged(object sender, EventArgs e)
		{
			this.textCustom.Enabled = this.radioCustom.Checked;
		}

		private void radioCustom_CheckedChanged(object sender, EventArgs e)
		{
			this.textCustom.Enabled = this.radioCustom.Checked;
		}

		private void checkAreaNumber_CheckedChanged(object sender, EventArgs e)
		{
			this.SelectThumbnail();
			this.textAreaNumber.Enabled = this.checkAreaNumber.Checked;
		}

		private void checkFloorNumber_CheckedChanged(object sender, EventArgs e)
		{
			this.SelectThumbnail();
			this.textFloorNumber.Enabled = this.checkFloorNumber.Checked;
		}

		private void checkLineAngle_CheckedChanged(object sender, EventArgs e)
		{
			this.textLineAngle.Enabled = this.checkLineAngle.Checked;
		}

		private void checkDisToPipe_CheckedChanged(object sender, EventArgs e)
		{
			this.textDisToPipe.Enabled = this.checkDisToPipe.Checked;
		}

		protected void SwithStatus()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X + 20, Cursor.Position.Y);
			this.SetFocusToMainWnd();
			this.PressESCKey();
		}

		protected void SetFocusToMainWnd()
		{
			int wMsg = 7;
			FrmMarkRiserpipe.SendMessage(MarkRiserPipeCommand.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		protected void PressESCKey()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FrmMarkRiserpipe.keybd_event((byte)bVk, 0, dwFlags, 0);
			FrmMarkRiserpipe.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void textCustom_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (textBox.Text == "" || !Regex.IsMatch(textBox.Text, "^[0-9a-zA-Z'_]+$"))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				textBox.Focus();
				textBox.SelectAll();
				return;
			}
		}

		private void textLineAngle_TextChanged(object sender, EventArgs e)
		{
		}

		private void textDisToPipe_TextChanged(object sender, EventArgs e)
		{
		}

		private void FrmMarkRiserpipe_FormClosed(object sender, FormClosedEventArgs e)
		{
			if (this.radioSinglePipe.Checked)
			{
				MarkRiserPipeData.Instance.TypeOfMarking = MarkType.singlePipeMarking;
			}
			if (this.radioMultiPipe.Checked)
			{
				MarkRiserPipeData.Instance.TypeOfMarking = MarkType.multiPipeMarking;
			}
			if (this.radioAutoIncrease.Checked)
			{
				MarkRiserPipeData.Instance.NumberCreating = CreateNumber.increaseWithSystem;
			}
			if (this.radioCustom.Checked)
			{
				MarkRiserPipeData.Instance.NumberCreating = CreateNumber.custom;
			}
			MarkRiserPipeData.Instance.IsUseAreaNumber = this.checkAreaNumber.Checked;
			MarkRiserPipeData.Instance.IsUseFloorNumber = this.checkFloorNumber.Checked;
			MarkRiserPipeData.Instance.IsUseAngle = this.checkLineAngle.Checked;
			MarkRiserPipeData.Instance.IsFixedDis = this.checkDisToPipe.Checked;
			MarkRiserPipeData.Instance.customNumber = this.textCustom.Text;
			MarkRiserPipeData.Instance.AreaNumber = this.textAreaNumber.Text;
			MarkRiserPipeData.Instance.FloorNumber = this.textFloorNumber.Text;
			MarkRiserPipeData.Instance.Angle = this.textLineAngle.Text;
			MarkRiserPipeData.Instance.Distance = this.textDisToPipe.Text;
			MarkRiserPipeData.Instance.IsCurrentLevel = this.rboCurrentLevel.Checked;
			MarkRiserPipeData.Instance.IsExistNum = this.checkBox1.Checked;
			this.SetFocusToMainWnd();
			this.PressESCKey();
			this.isPicking = false;
		}

		private void FrmMarkRiserpipe_MouseLeave(object sender, EventArgs e)
		{
			this.textCustom_Validated(null, null);
			this.textDisToPipe_Validated(null, null);
			this.textAreaNumber_Validated(null, null);
			this.textFloorNumber_Validated(null, null);
			this.textLineAngle_Validated(null, null);
		}

		public bool CheckBeforeDraw()
		{
			if (this.radioCustom.Checked && !Regex.IsMatch(this.textCustom.Text, "^[0-9a-zA-Z'_]+$"))
			{
				YJKMessageBox.Warning("自定义值不能为空且必须由英文字母、数字或'_组成！");
				base.Focus();
				this.textCustom.Focus();
				this.textCustom.SelectAll();
				return false;
			}
			if (this.checkAreaNumber.Checked && !this.RegexText(this.textAreaNumber.Text))
			{
				YJKMessageBox.Warning("区号值不能为空且必须由英文字母、数字或'_组成！");
				this.textAreaNumber.Focus();
				this.textAreaNumber.SelectAll();
				return false;
			}
			if (this.checkFloorNumber.Checked && !this.RegexText(this.textFloorNumber.Text))
			{
				YJKMessageBox.Warning("楼号值不能为空且必须由英文字母、数字或'_组成！");
				this.textFloorNumber.Focus();
				this.textFloorNumber.SelectAll();
				return false;
			}
			if (this.checkLineAngle.Checked && string.IsNullOrEmpty(this.textLineAngle.Text))
			{
				this.textLineAngle.Text = MarkRiserPipeData.Instance.Angle;
			}
			else if (this.checkLineAngle.Checked && !string.IsNullOrEmpty(this.textLineAngle.Text))
			{
				try
				{
					double num = Convert.ToDouble(this.textLineAngle.Text);
					if (num < 0.0 || num > 90.0)
					{
						YJKMessageBox.Error("引线角度值必须输入0-90之间的角度值!");
						base.Focus();
						this.textLineAngle.Focus();
						return false;
					}
				}
				catch (Exception)
				{
					YJKMessageBox.Error("引线角度值必须是数字，请重新输入!");
					base.Focus();
					this.textLineAngle.Focus();
					return false;
				}
			}
			if (this.checkDisToPipe.Checked && string.IsNullOrEmpty(this.textDisToPipe.Text))
			{
				this.textDisToPipe.Text = MarkRiserPipeData.Instance.Distance;
			}
			else if (this.checkDisToPipe.Checked && !string.IsNullOrEmpty(this.textDisToPipe.Text))
			{
				try
				{
					if (Convert.ToDouble(this.textDisToPipe.Text) < 0.0)
					{
						YJKMessageBox.Error("距管垂直距离值必须大于0，请重新输入!");
						base.Focus();
						this.textDisToPipe.Focus();
						return false;
					}
				}
				catch (Exception)
				{
					YJKMessageBox.Error("距管垂直距离值必须是数字，请重新输入!");
					base.Focus();
					this.textDisToPipe.Focus();
					return false;
				}
			}
			return true;
		}

		private void textCustom_Validated(object sender, EventArgs e)
		{
			if (!Regex.IsMatch(this.textCustom.Text, "^[0-9a-zA-Z'_]+$"))
			{
				YJKMessageBox.Warning("自定义值不能为空且必须由英文字母、数字或'_组成！");
				this.textCustom.Focus();
				this.textCustom.SelectAll();
				return;
			}
		}

		private void textLineAngle_Validated(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.textLineAngle.Text))
			{
				this.textLineAngle.Text = MarkRiserPipeData.Instance.Angle;
				this.textLineAngle.Focus();
			}
			try
			{
				double num = Convert.ToDouble(this.textLineAngle.Text);
				if (num < 0.0 || num > 90.0)
				{
					YJKMessageBox.Error("引线角度值必须输入0-90之间的角度值!");
					this.textLineAngle.Focus();
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Error("引线角度值必须是数字，请重新输入!");
				this.textLineAngle.Focus();
			}
		}

		private void textDisToPipe_Validated(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.textDisToPipe.Text))
			{
				this.textDisToPipe.Text = MarkRiserPipeData.Instance.Distance;
				this.textDisToPipe.Focus();
			}
			try
			{
				if (Convert.ToDouble(this.textDisToPipe.Text) < 0.0)
				{
					YJKMessageBox.Error("距管垂直距离值必须大于0，请重新输入!");
					this.textDisToPipe.Focus();
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Error("距管垂直距离值必须是数字，请重新输入!");
				this.textDisToPipe.Focus();
			}
		}

		private bool RegexText(string strText)
		{
			return Regex.IsMatch(strText, "^[0-9a-zA-Z'_]+$");
		}

		private void textAreaNumber_Validated(object sender, EventArgs e)
		{
			if (!this.RegexText(this.textAreaNumber.Text))
			{
				YJKMessageBox.Warning("区号值不能为空且必须由英文字母、数字或'_组成！");
				this.textAreaNumber.Focus();
				this.textAreaNumber.SelectAll();
				return;
			}
		}

		private void textFloorNumber_Validated(object sender, EventArgs e)
		{
			if (!this.RegexText(this.textFloorNumber.Text))
			{
				YJKMessageBox.Warning("楼号值不能为空且必须由英文字母、数字或'_组成！");
				this.textFloorNumber.Focus();
				this.textFloorNumber.SelectAll();
				return;
			}
		}

		private void FrmMarkRiserpipe_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		private void textAreaNumber_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
			if (textBox.Text == "" || !Regex.IsMatch(textBox.Text, "^[0-9a-zA-Z'_]+$"))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				textBox.Focus();
				textBox.SelectAll();
				return;
			}
		}

		private void textCustom_KeyPress(object sender, KeyPressEventArgs e)
		{
			System.Windows.Forms.TextBox textBox = (System.Windows.Forms.TextBox)sender;
			if (!Regex.IsMatch(e.KeyChar.ToString(), "^[0-9a-zA-Z'_\\b]+$"))
			{
				YJKMessageBox.Warning("文本框值不能为空且必须由英文字母、数字或'_组成！");
				textBox.Focus();
				e.Handled = true;
			}
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			if (this.checkBox1.Checked)
			{
				this.checkAreaNumber.Enabled = false;
				this.checkFloorNumber.Enabled = false;
				this.textAreaNumber.Enabled = false;
				this.textFloorNumber.Enabled = false;
				return;
			}
			this.checkAreaNumber.Enabled = true;
			this.checkFloorNumber.Enabled = true;
			this.textAreaNumber.Enabled = true;
			this.textFloorNumber.Enabled = true;
		}

		public bool isPicking = true;

		private ControlInfoManager m_thisManger;
	}
}
